package com.whosly.disclosure.early.handler.infra.yaml;

import org.yaml.snakeyaml.nodes.*;

/**
 * Default YAML tuple processor.
 *
 * @author fengyang <deep_blue_yang@126.com>
 * @date 2020-10-09  10:21
 * @title: DefaultYamlTupleProcessor
 */
public final class DefaultYamlTupleProcessor {

    /**
     * Process node tuple.
     *
     * @param nodeTuple node tuple
     * @return processed node tuple
     */
    public NodeTuple process(final NodeTuple nodeTuple) {
        return isUnsetNodeTuple(nodeTuple.getValueNode()) ? null : nodeTuple;
    }

    private boolean isUnsetNodeTuple(final Node valueNode) {
        return isNullNode(valueNode) || isEmptyCollectionNode(valueNode);
    }

    private boolean isNullNode(final Node valueNode) {
        return Tag.NULL.equals(valueNode.getTag());
    }

    private boolean isEmptyCollectionNode(final Node valueNode) {
        return valueNode instanceof CollectionNode && (isEmptySequenceNode(valueNode) || isEmptyMappingNode(valueNode));
    }

    private boolean isEmptySequenceNode(final Node valueNode) {
        return Tag.SEQ.equals(valueNode.getTag()) && ((SequenceNode) valueNode).getValue().isEmpty();
    }

    private boolean isEmptyMappingNode(final Node valueNode) {
        return Tag.MAP.equals(valueNode.getTag()) && ((MappingNode) valueNode).getValue().isEmpty();
    }
}